package cn.easy.rest.util;

import cn.easy.pojo.User;
import cn.easy.rest.dao.JedisClient;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@ContextHierarchy(@ContextConfiguration(name = "parent", locations = {"classpath:/META-INF/spring/applicationContext-*.xml"}))

public class UserKeyUtil {
    public static String getUserInfoFromToken(HttpServletRequest request, JedisClient jedisClient) {
        String apiKey = request.getHeader("api-key");
        if (apiKey != null) {
            return apiKey;
        }
        String key = request.getHeader("easylink_user_token");
        if (key == null) {
            return null;
        }
        ObjectMapper objectMapper = new ObjectMapper();
        String date = jedisClient.get(key);
        if(date==null){
            return null;
        }
        User user = null;
        try {
            user = objectMapper.readValue(date, User.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (user == null) {
            return null;
        }
        return user.getUserKey();
    }
}
